

#import "FAMMineDeficitWeakView.h"
@interface FAMMineDeficitWeakView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *FAMYearLabel;
@property (weak, nonatomic) IBOutlet UIPickerView *FAMPickerView;
@property (nonatomic, strong)NSMutableArray *FAMWeekArray;
@property (nonatomic, strong)NSMutableArray *FAMSelectedWeekArray;
@end
@implementation FAMMineDeficitWeakView
- (NSMutableArray *)FAMWeekArray{
    if (!_FAMWeekArray) {
        _FAMWeekArray = [NSMutableArray array];
    }
    return _FAMWeekArray;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FAMMineDeficitWeakView" owner:self options:nil];
        self = array.firstObject;
        [self FAM_showUi];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
- (void)FAM_showUi{
    self.FAMPickerView.delegate = self;
    self.FAMPickerView.dataSource = self;
    NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger currentYear = [[calendar components:NSCalendarUnitYear fromDate:currentDate] year];
    self.FAMYearLabel.text = [NSString stringWithFormat:@"%ld",currentYear];
    
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setYear:currentYear];
    [dateComponents setMonth:1];
    [dateComponents setDay:1];
    [dateComponents setWeekday:[calendar firstWeekday]];
    NSDate *startOfYear = [calendar dateFromComponents:dateComponents];
    NSRange weekRange = [calendar rangeOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYear forDate:startOfYear];
    for (int i = 0; i<weekRange.length; i++) {
        NSMutableArray *weakArray = [self FAM_addWeak:i currentYear:currentYear];
        if (weakArray.count != 0) {
            [self.FAMWeekArray addObject:weakArray];
        }
    }
    [self.FAMPickerView selectRow:self.FAMWeekArray.count/2 inComponent:0 animated:YES];
    self.FAMSelectedWeekArray = self.FAMWeekArray[self.FAMWeekArray.count/2];
}
- (NSMutableArray *)FAM_addWeak:(int)i currentYear:(NSInteger)currentYear{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = currentYear;
    components.weekday = 0;
    components.weekOfYear = i;
    NSDate *startOfWeek = [calendar dateFromComponents:components];
    NSDate *endOfWeek;
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 7; i++) {
        endOfWeek = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:startOfWeek options:0];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyyMMdd"];
        NSString *weakString = [dateFormatter stringFromDate:endOfWeek];
        NSString *firstChar = [weakString substringToIndex:4];
        if ([firstChar isEqualToString:[NSString stringWithFormat:@"%ld",currentYear]]) {
            [tempArray addObject:endOfWeek];
        }
        
    }
    return tempArray;
}

#pragma mark ---- UIPickerViewDelegate UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.FAMWeekArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSMutableArray *tempArray = self.FAMWeekArray[row];
    NSDate *beginDate = tempArray.firstObject;
    NSDate *endDate = tempArray.lastObject;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM.dd"];
    NSString *begin_date = [dateFormatter stringFromDate:beginDate];
    NSString *end_date = [dateFormatter stringFromDate:endDate];
    return [NSString stringWithFormat:@"%@-%@",begin_date,end_date];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.FAMSelectedWeekArray = self.FAMWeekArray[row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 60;
}

- (IBAction)FAM_confirmButtonClick:(UIButton *)sender {
    if (self.block) {
        self.block(self.FAMSelectedWeekArray.firstObject, self.FAMSelectedWeekArray.lastObject);
    }
    [self removeFromSuperview];
}

@end
